<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
require('echarts/theme/macarons')
import resize from '@/components/Charts/mixins/resize'

export default {
  name: 'AuthCountLine',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      timeArray: [],
      passWordArray: [],
      smsArray: [],
      tokenArray: [],
      uKeyArray: [],
      smsAndpassWordArray: [],
      otherOneArray: [],
      otherTwoArray: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.getData()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, 'macarons')
      const initOption = {
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.8)', // 设置背景图片 rgba格式
          color: 'black',
          borderWidth: '1', // 边框宽度设置1
          borderColor: 'gray', // 设置边框颜色
          textStyle: {
            color: 'black' // 设置文字颜色
          }
        },
        legend: {
          data: ['单口令', '单短信', '动态令牌', 'Ukey', '短信验证码结合口令', '占位1', '占位2']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true // 在计算这些距离时是否加入标签
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            // 同个类目轴上配置了相同的stack值后，后一个系列的值就会在前一个的值上相加
            name: '单口令',
            type: 'line',
            // stack: '总量',
            // 这个主题默认smooth为true
            smooth: false
          },
          {
            name: '单短信',
            type: 'line',
            // stack: '总量',
            smooth: false
          },
          {
            name: '动态令牌',
            type: 'line',
            // stack: '总量',
            smooth: false
          },
          {
            name: 'Ukey',
            type: 'line',
            // stack: '总量',
            smooth: false
          },
          {
            name: '短信验证码结合口令',
            type: 'line',
            // stack: '总量',
            smooth: false
          },
          {
            name: '占位1',
            type: 'line',
            // stack: '总量',
            smooth: false
          },
          {
            name: '占位2',
            type: 'line',
            // stack: '总量',
            smooth: false
          }
        ]
      }
      this.chart.setOption(initOption)
    },
    getData() {
      this.timeArray = ['2021/08/26', '2021/08/27', '2021/08/28', '2021/08/29', '2021/08/30', '2021/08/31', '2021/09/01']
      this.passWordArray = ['4234', '3134', '3134', '3316', '3144', '3241', '3524']
      this.smsArray = ['3215', '4321', '3124', '4131', '4353', '4123', '5131']
      this.tokenArray = ['2432', '3125', '4321', '5432', '4145', '5438', '3156']
      this.uKeyArray = ['3125', '3265', '4312', '3125', '5775', '3213', '4234']
      this.smsAndpassWordArray = ['4326', '5356', '5574', '5456', '5342', '7864', '5354']
      this.otherOneArray = ['3214', '5436', '5657', '4376', '5654', '5437', '5897']
      this.otherTwoArray = ['5435', '4239', '5657', '4326', '5514', '5234', '5354']
      this.updateChart()
    },
    updateChart() {
      const updateOption = {
        series: [
          { data: this.timeArray },
          { data: this.passWordArray },
          { data: this.smsArray },
          { data: this.tokenArray },
          { data: this.uKeyArray },
          { data: this.smsAndpassWordArray },
          { data: this.otherOneArray },
          { data: this.otherTwoArray }
        ]
      }
      this.chart.setOption(updateOption)
    }
  }
}
</script>

<style scoped>

</style>
